﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 原料异议处理 : System.Web.UI.Page
    {
        string sql =
@"select 原料异议库.ID
,原料.品名 as 原料类别
,原料供货单位.名称 as 供货单位
,原料异议库.使用单位ID as 分厂
,原料异议库.炉号
,原料异议库.数量
,原料异议库.重量
,原料异议库.存储区域
,原料异议库.日期
,原料异议库.已处理
,原料异议库.处理日期
from 原料异议库
left outer join 原料 on 原料异议库.原料ID=原料.ID
left outer join 原料供货单位 on 原料异议库.供货单位ID=原料供货单位.ID";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {
                string opp3 = (string)Session["OPP3"];
#if DEBUG
                opp3 = ",1,2,3,4,5,6,101,102,103,104,105,106,";
#endif
                if (opp3 == null || opp3 == ",")
                {
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = false;
                    this.Panel3.Visible = false;
                    this.Message.Text = "·权限不足";
                    return;
                }
                else
                {
                    if (opp3.StartsWith(","))
                    {
                        opp3 = opp3.Substring(1);
                    }
                    if (opp3.EndsWith(","))
                    {
                        opp3 = opp3.Substring(0, opp3.Length - 1);
                    }
                    
                    this.SqlDataSource2.SelectCommand = "select [ID],[品名]+'('+[钢级]+')' as [品名] from [原料] where [ID] in (" + opp3 + ")";
                    this.YuanLiao.DataBind();
                    this.YuanLiao2.DataBind();
                    this.GongHuo.DataBind();
                    this.GongHuo2.DataBind();
                    this.Date1.Text = this.Date2.Text = DateTime.Today.ToLongDateString();
                    this.Button_Query_Click(null, null);
                }
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
            this.Message.Text = "·准备就绪";
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string con = " where 原料异议库.原料ID=" + this.YuanLiao.Text;


            if (this.GongHuo.Text != "0")
            {
                con += " and 原料异议库.供货单位ID=" + this.GongHuo.Text;
            }

            if (this.FenChang.Text != "0")
            {
                con += " and 原料异议库.使用单位ID=" + this.FenChang.Text;
            }

            if (this.LuHao.Text != "")
            {
                con += " and 原料异议库.炉号='" + this.LuHao.Text.Replace("'", "") + "'";
            }

            DateTime d1, d2;

            if (this.Date1.Text != "")
            {
                if (DateTime.TryParse(this.Date1.Text, out d1))
                {
                    con += " and 原料异议库.日期>='" + d1.ToShortDateString() + "'";
                }
            }

            if (this.Date2.Text != "")
            {
                if (DateTime.TryParse(this.Date2.Text, out d2))
                {
                    con += " and 原料异议库.日期<='" + d2.ToShortDateString() + "'";
                }
            }

            con += " order by 原料异议库.ID desc";
            this.SqlDataSource1.SelectCommand = "";

            this.SqlDataSource1.SelectCommand = sql + con;
            this.SqlText.Text = sql + con;
            this.GridView1.SelectedIndex = -1;
            this.Panel3.Visible = false;

            this.Button_Delete.Visible = false;

            this.SelectedID.Text = "";
        }
        protected void Button_New_Click(object sender, EventArgs e)
        {
            this.Button_ShowQuery.Text = "显示查询栏";
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
            this.Panel3.GroupingText = "录 入";
            this.LuHao2.Text = "";
            this.ZhiShu2.Text = "";
            this.Zhong2.Text = "";
            this.Date3.Text = DateTime.Today.ToLongDateString();
            this.KuQu2.Text = "";
            this.BeiZhu2.Text = "";
            this.CurrentID.Text = "0";
            this.Date4.Text = "";
            this.ChuLi2.Text = "";
        }

        protected void YiChuLi_OnCheckedChanged(object sender, EventArgs e)
        {
            if (this.YiChuLi.Checked)
            {
                this.Date4.Enabled = true;
                this.ChuLi2.Enabled = true;
                if (this.Date4.Text == "")
                {
                    this.Date4.Text = DateTime.Today.ToLongDateString();
                }
            }
            else
            {
                this.Date4.Enabled = false;
                this.ChuLi2.Enabled = false;
            }
            
        }

        protected void Button_Cancel_Click(object sender, EventArgs e)
        {
            this.Button_ShowQuery.Text = "隐藏查询栏";
            this.Panel2.Visible = true;
            this.Panel3.Visible = false;
            this.Button_Delete.Visible = false;
            this.GridView1.SelectedIndex = -1;
            this.SelectedID.Text = "";
        }
        protected void Button_Save_Click(object sender, EventArgs e)
        {
            int kq;
            if (!int.TryParse(this.KuQu2.Text, out kq))
            {
                this.Message.Text = "·库区需录入一个数字";
                return;
            }
            int zs;
            if (!int.TryParse(this.ZhiShu2.Text, out zs))
            {
                this.Message.Text = "·支数需录入一个数字";
                return;
            }
            decimal zl;
            if (!decimal.TryParse(this.Zhong2.Text, out zl))
            {
                this.Message.Text = "·重量需录入一个数字";
                return;
            }
            DateTime d;
            if (!DateTime.TryParse(this.Date3.Text, out d))
            {
                this.Message.Text = "·日期录入格式有误";
                return;
            }

            DateTime d2=DateTime.Today;
            if (this.YiChuLi.Checked)
            {
                if (!DateTime.TryParse(this.Date4.Text, out d2))
                {
                    this.Message.Text = "·日期录入格式有误";
                    return;
                }
            }
            if (this.CurrentID.Text == "0")
            {
                if (this.YiChuLi.Checked)
                {
                    this.SqlDataSource1.UpdateCommand =
    "insert into 原料异议库(原料ID,供货单位ID,使用单位ID,炉号,数量,重量,存储区域,日期,备注,已处理,处理日期,处理方式)values("
    + this.YuanLiao2.Text + "," + this.GongHuo2.Text + "," + this.FenChang2.Text
    + ",'" + this.LuHao2.Text.Replace("'", "") + "'," + zs + "," + zl.ToString() + "," + kq + ",'"
    + d.ToShortDateString() + "','" + this.BeiZhu2.Text.Replace("'", "") + "',1,'" + d2.ToShortDateString() + "','" + this.ChuLi2.Text.Replace("'", "") + "')";
                }
                else
                {
                    this.SqlDataSource1.UpdateCommand =
"insert into 原料异议库(原料ID,供货单位ID,使用单位ID,炉号,数量,重量,存储区域,日期,备注)values("
+ this.YuanLiao2.Text + "," + this.GongHuo2.Text + "," + this.FenChang2.Text
+ ",'" + this.LuHao2.Text.Replace("'", "") + "'," + zs + "," + zl.ToString() + "," + kq + ",'"
+ d.ToShortDateString() + "','" + this.BeiZhu2.Text.Replace("'", "") + "')";
                }
            }
            else
            {
                if (this.YiChuLi.Checked)
                {
                    this.SqlDataSource1.UpdateCommand =
    "update 原料异议库 set 原料ID=" + this.YuanLiao2.Text
    + ",供货单位ID=" + this.GongHuo2.Text
    + ",使用单位ID=" + this.FenChang2.Text
    + ",炉号='" + this.LuHao2.Text.Replace("'", "")
    + "',数量=" + zs
    + ",重量=" + zl.ToString()
    + ",存储区域=" + kq
    + ",日期='" + d.ToShortDateString()
    + "',备注='" + this.BeiZhu2.Text.Replace("'", "")
    + "',已处理=1,处理日期='"+d2.ToShortDateString()
    +"',处理方式='" + this.ChuLi2.Text.Replace("'", "")+"' where id=" + this.CurrentID.Text;
                }
                else
                {
                    this.SqlDataSource1.UpdateCommand =
    "update 原料异议库 set 原料ID=" + this.YuanLiao2.Text
    + ",供货单位ID=" + this.GongHuo2.Text
    + ",使用单位ID=" + this.FenChang2.Text
    + ",炉号='" + this.LuHao2.Text.Replace("'", "")
    + "',数量=" + zs
    + ",重量=" + zl.ToString()
    + ",存储区域=" + kq
    + ",日期='" + d.ToShortDateString()
    + "',备注='" + this.BeiZhu2.Text.Replace("'", "")
    + "',已处理=0,处理日期=null where id=" + this.CurrentID.Text;
                }
            }
            //Response.Write(this.SqlDataSource1.UpdateCommand);
            int n = this.SqlDataSource1.Update();
            if (n > 0)
            {
                this.Message.Text = "·保存成功";
                this.GridView1.SelectedIndex = -1;
                this.SelectedID.Text = "";
                this.Button_Delete.Visible = false;
                Button_ShowQuery_Click(null, null);
            }
            else
            {
                this.Message.Text = "·保存失败";
            }
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteCommand = "delete from 原料异议库 where id="+this.SelectedID.Text;
            this.SqlDataSource1.Delete();
            this.Panel3.Visible = false;
        }

        protected void Button_ShowQuery_Click(object sender, EventArgs e)
        {
            if (this.Button_ShowQuery.Text == "隐藏查询栏")
            {
                this.Button_ShowQuery.Text = "显示查询栏";
                this.Panel2.Visible = false;
            }
            else
            {
                this.Button_ShowQuery.Text = "隐藏查询栏";
                this.Panel2.Visible = true;
            }
            this.Panel3.Visible = false;
        }
        protected void ShowDetail()
        {

            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            Cmd.CommandText = "select 原料ID,供货单位ID,使用单位ID,炉号,数量,重量,存储区域,日期,备注,处理日期,处理方式,已处理 from 原料异议库 where id=" + this.SelectedID.Text;

            Cmd.CommandType = CommandType.Text;
            SqlDataReader Reader = Cmd.ExecuteReader();

            if (Reader.Read())
            {
                this.YuanLiao2.Text = ((int)Reader["原料ID"]).ToString();
                this.GongHuo2.Text = ((int)Reader["供货单位ID"]).ToString();
                this.FenChang2.Text = ((int)Reader["使用单位ID"]).ToString();
                this.LuHao2.Text = (string)Reader["炉号"];
                this.ZhiShu2.Text = ((int)Reader["数量"]).ToString();
                this.Zhong2.Text = ((decimal)Reader["重量"]).ToString();
                this.KuQu2.Text = ((int)Reader["存储区域"]).ToString();
                this.Date3.Text = ((DateTime)Reader["日期"]).ToLongDateString();
                if (!Reader.IsDBNull(8))
                {
                    this.BeiZhu2.Text = (string)Reader["备注"];
                }
                else
                {
                    this.BeiZhu2.Text = "";
                }
                if (!Reader.IsDBNull(9))
                {
                    this.Date4.Text = ((DateTime)Reader["处理日期"]).ToLongDateString();
                }
                else
                {
                    this.Date4.Text = "";
                }
                if (!Reader.IsDBNull(10))
                {
                    this.ChuLi2.Text = (string)Reader["处理方式"];
                }
                else
                {
                    this.ChuLi2.Text = "";
                }

                if ((bool)Reader["已处理"])
                {
                    this.Date4.Enabled = true;
                    this.ChuLi2.Enabled = true;
                }
                else
                {
                    this.Date4.Enabled = false;
                    this.ChuLi2.Enabled = false;
                }

            }

            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();


            this.Button_ShowQuery.Text = "显示查询栏";
            this.Panel2.Visible = false;
            this.Panel3.Visible = true;
            this.Panel3.GroupingText = "修 改";
            this.CurrentID.Text = this.SelectedID.Text;

        }

        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }

        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {

            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SelectedID.Text = this.GridView1.SelectedDataKey[0].ToString();

            if (!(bool)this.GridView1.SelectedDataKey[1])
            {
                this.Button_Delete.Visible = true;
            }
            else
            {
                this.Button_Delete.Visible = false;
            }
            ShowDetail();

        }
    }
}
